﻿// $Id: Question.cs 10 2008-02-29 15:58:02Z mcartoixa $
/*
 *  Copyright (C) 2007 Noury Solutions
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 3 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */
/*! \file
 * \brief Definition of \c Salamanca.DataActivities.UI.Question.
 *
 * \author $Author: mcartoixa $
 * \date $Date: 2008-02-29 16:58:02 +0100 (ven., 29 févr. 2008) $
 * \version $Revision: 10 $
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace Salamanca.DataActivities.UI
{



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Arguments for a question event.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    public sealed class QuestionEventArgs:
        EventArgs
    {
        /// <summary>Initializes a new instance of the <see cref="QuestionEventArgs"/> class.</summary>
        /// <param name="controller">The controller.</param>
        public QuestionEventArgs(IActivityController controller)
        {
            _Controller=controller;
        }

        /// <summary>Gets the controller.</summary>
        /// <value>The controller.</value>
        public IActivityController Controller
        {
            get
            {
                return _Controller;
            }
        }

        /// <summary>The activity controller.</summary>
        private IActivityController _Controller;
    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Interface implemented by a question.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    public interface IQuestion
    {

        /// <summary>Initializes the question.</summary>
        void Initialize(IActivityController controller);

        /// <summary>Asks the question.</summary>
        ActivityStateResult Ask(IActivityController controller);

        /// <summary>Answers the question.</summary>
        void Answer(object answer, object activityData);

        /// <summary>Terminates the question.</summary>
        void Terminate(IActivityController controller);

        /// <summary>Raised when the question is initialized.</summary>
        event EventHandler<QuestionEventArgs> Initialized;
        /// <summary>Raised when the question is terminated.</summary>
        event EventHandler<QuestionEventArgs> Terminated;
        /// <summary>Raised when the question is about to be answered.</summary>
        event EventHandler<AnswerEventArgs> Answering;

    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Base implementation of a question.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    public abstract class Question:
        IQuestion
    {

        /// <summary>Initializes a new <see cref="Question" /> instance.</summary>
        protected Question()
        {
        }

        /// <summary>Raises the <see cref="Initialized" /> event.</summary>
        protected virtual void OnInitialized(QuestionEventArgs e)
        {
            if (Initialized!=null)
                Initialized(this, e);
        }

        /// <summary>Initializes the question.</summary>
        private void Initialize(IActivityController controller)
        {
            OnInitialized(new QuestionEventArgs(controller));
        }

        /// <summary>Initializes the question.</summary>
        void IQuestion.Initialize(IActivityController controller)
        {
            Initialize(controller);
        }

        /// <summary>Asks the question.</summary>
        public abstract ActivityStateResult Ask(IActivityController controller);

        /// <summary>Asks the question.</summary>
        ActivityStateResult IQuestion.Ask(IActivityController controller)
        {
            return Ask(controller);
        }

        /// <summary>Raises the <see cref="Answering" /> event.</summary>
        protected virtual void OnAnswering(AnswerEventArgs e)
        {
            if (Answering!=null)
                Answering(this, e);
        }

        /// <summary>Answers the question.</summary>
        private void Answer(object answer, object data)
        {
            OnAnswering(new AnswerEventArgs(answer, data));
        }

        /// <summary>Answers the question.</summary>
        void IQuestion.Answer(object answer, object data)
        {
            Answer(answer, data);
        }

        /// <summary>Raises the <see cref="Terminated" /> event.</summary>
        protected virtual void OnTerminated(QuestionEventArgs e)
        {
            if (Terminated!=null)
                Terminated(this, e);
        }

        /// <summary>Terminates the question.</summary>
        private void Terminate(IActivityController controller)
        {
            OnTerminated(new QuestionEventArgs(controller));
        }

        /// <summary>Terminates the question.</summary>
        void IQuestion.Terminate(IActivityController controller)
        {
            Terminate(controller);
        }

        /// <summary>Raised when the state is initialized.</summary>
        public event EventHandler<QuestionEventArgs> Initialized;
        /// <summary>Raised when the state is terminated.</summary>
        public event EventHandler<QuestionEventArgs> Terminated;
        /// <summary>Raised when the question is about to be answered.</summary>
        public event EventHandler<AnswerEventArgs> Answering;
    }

}
